package com.topnav.mc.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.js.lib.annotation.ApiLogger;
import com.js.lib.result.Result;
import com.topnav.mc.admin.service.IDeviceService;
import com.topnav.mc.alarm.dao.mapper.AlarmInfoMapper;
import com.topnav.mc.alarm.entity.AlarmInfo;
import com.topnav.mc.alarm.service.IAlarmInfoService;
import com.topnav.mc.document.dao.mapper.DocumentMapper;
import com.topnav.mc.document.entity.Document;
import com.topnav.mc.document.service.IDocumentService;
import com.topnav.mc.uitls.MapUtil;
import com.topnav.mc.uitls.QueryPage;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(value = "home", tags = "首面展示")
@RestController
@CrossOrigin
@RequestMapping("home")
@Slf4j
public class HomeController {

    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private DocumentMapper documentMapper;
    @Autowired
    private IAlarmInfoService alarmInfoService;
    @Autowired
    private IDocumentService documentService;

    /**
     * 根据用户输入的值查询4G执法仪，上级带机构的
     * 给前端 Tree组件使用
     * */
    @ApiLogger(apiName="/countDevices")
    @ApiOperation(value = "统计各类型的注册设备数")
    @GetMapping("/countDevices")
    @ResponseBody
    public Result countDevice() {
        Map<String, Object> data = new HashMap<>();
        data.put("common",0);
        data.put("mobile",0);
        data.put("camera",0);
        List<Map<String, Object>> deviceList = deviceService.countDevices();
        Integer type = null;
        for(Map<String, Object>  o : deviceList){
            type = Integer.valueOf(o.get("type").toString());
            if (type == 1){
                data.put("common",Integer.valueOf(o.get("total").toString()));
            }else if (type == 2){
                data.put("mobile",Integer.valueOf(o.get("total").toString()));
            }else if (type == 3){
                data.put("camera",Integer.valueOf(o.get("total").toString()));
            }else{

            }
        }
        return Result.ok(data);
    }
    @ApiLogger(apiName="/documentStatistics")
    @ApiOperation(value = "上传文件统计")
    @GetMapping("/documentStatistics/{type}")
    @ResponseBody
    public Result countDocument(@PathVariable Integer type) {
        Date dateBegin=null;
        Date dateEnd=null;
        Calendar calendar = Calendar.getInstance();
        if (type == 1){
            /** 昨日 */
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            dateBegin=set000000(calendar).getTime();
            dateEnd=set235959(calendar).getTime();
        }else if (type == 2){
            /** 今日 */
            dateBegin=set000000(calendar).getTime();
            dateEnd=set235959(calendar).getTime();
        }else if (type == 3){
            /** 本月 */
            calendar.add(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            dateBegin=set000000(calendar).getTime();

            calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, 1);
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            dateEnd=set235959(calendar).getTime();
        }else if (type == 4){
            /** 上月 */
            calendar.set(Calendar.MONDAY, calendar.get(Calendar.MONDAY) - 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            dateBegin=set000000(calendar).getTime();

            calendar = Calendar.getInstance();
            calendar.set(Calendar.MONDAY, calendar.get(Calendar.MONDAY) - 1);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            dateEnd=set235959(calendar).getTime();
        }else if (type == 5){
            /** 本年 */
            calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMinimum(Calendar.DAY_OF_YEAR));
            dateBegin=set000000(calendar).getTime();

            calendar = Calendar.getInstance();
            calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
            calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            dateEnd=set235959(calendar).getTime();
        }
        log.debug("begin: {} -- end: {}",dateBegin,dateEnd);

        return Result.ok(documentMapper.statisticsDocument(dateBegin,dateEnd));
        //DocumentStatisticsBean
    }

    /**
     * 设置0点时间
     * @param calendar
     * @return
     */
    private Calendar set000000(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar;
    }

    /**
     * 设置24点时间
     * @param calendar
     * @return
     */
    private Calendar set235959(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar;
    }

    /**
     * 最新上传的视频
     * @return
     */
    @ApiLogger(apiName="/searchDocument")
    @ApiOperation(value = "最新上传的视频")
    @GetMapping("/searchDocument")
    @ResponseBody
    public Result searchDocument() {
        Page<Document> page = new Page<>();
        page.setCurrent(1);
        page.setPages(1);
        page.setSize(10L);

        Page datas = documentService.searchDocument(page,new Document());
        return Result.ok(datas);
    }

    /**
     * 最新关联的警情
     * @return
     */
    @ApiLogger(apiName="/searchAlarm")
    @ApiOperation(value = "最新关联的警情")
    @GetMapping("/searchAlarm")
    @ResponseBody
    public Result searchAlarm() {
        Page<AlarmInfo> page = new Page<>();
        page.setCurrent(1);
        page.setPages(1);
        page.setSize(10L);

        Page datas = alarmInfoService.searchAlarm(page,new AlarmInfo());
        return Result.ok(datas);
    }
}
